//
//  DKHomeLoanDetailController.m
//  DanKU
//
//  Created by 吴斌 on 2023/10/6.
//

#import "DKHomeLoanDetailController.h"
#import "DKAuthLoanDetailCell.h"
#import "DKHomeLoanScuessController.h"
@interface DKHomeLoanDetailController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView * homeLoanDetailTableView;
@property (nonatomic, strong)DKLoanDetailModel * dk_LoanDetailMdl;
@end

@implementation DKHomeLoanDetailController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    [self dk_addSubView];
    [self makeNavBack];
    [self requestTotalLoanInfo];
    [self requestUserBrhavRecordApplyLoan:DK_ActionEnter withParmDic:@{}];

    //获取拓展信息
    [[DKAuthExtHelper defaultManager] checkExtExpired];
    //风控埋点
    [self requestBeginLoan:NO];
}
- (void)dk_addSubView{
    UIImageView* dk_AuthLoanDetailHeadImgView = [[UIImageView alloc] init];
    dk_AuthLoanDetailHeadImgView.userInteractionEnabled = YES;
    dk_AuthLoanDetailHeadImgView.image = DK_GetImage(@"dk_LoanDetailHeadBg");
    [self.view addSubview:dk_AuthLoanDetailHeadImgView];
    [dk_AuthLoanDetailHeadImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@(DK_IsWidthScale(0)));
        make.height.equalTo(@(DK_IsWidthScale(220)));
    }];
    self.homeLoanDetailTableView = [[UITableView alloc] init];
    self.homeLoanDetailTableView.backgroundColor = UIColor.clearColor;
    self.homeLoanDetailTableView.delegate = self;
    self.homeLoanDetailTableView.dataSource = self;
    self.homeLoanDetailTableView.estimatedRowHeight = 0;
    self.homeLoanDetailTableView.estimatedSectionHeaderHeight = 0;
    self.homeLoanDetailTableView.estimatedSectionFooterHeight = 0;
    self.homeLoanDetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.homeLoanDetailTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    self.homeLoanDetailTableView.showsVerticalScrollIndicator = NO;
    self.homeLoanDetailTableView.shouldIgnoreScrollingAdjustment = YES;
    self.homeLoanDetailTableView.shouldRestoreScrollViewContentOffset = YES;
    self.homeLoanDetailTableView.clipsToBounds = YES;
    [self.view addSubview:self.homeLoanDetailTableView];
    [self.homeLoanDetailTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(DK_IsWidthScale(0)));
    }];
}
- (void)requestBeginLoan:(BOOL)isSucuess{
    if(isSucuess){
        [DKPublicHttp apiUserRiskForAPI:@"END_LOAN" complete:^{
            
        } fail:^{
            
        }];
    }else{
        [DKPublicHttp apiUserRiskForAPI:@"BEGIN_LOAN" complete:^{
            
        } fail:^{
            
        }];
    }
}
- (void)requestTotalLoanInfo{
    WeakSelf(self)
    NSDictionary * parmDic;
    NSMutableArray *totalLoanInfos = [[NSMutableArray alloc] init];
    if(self.dk_UserProductPackageMdl){
        NSDictionary *productPackageDic = [NSDictionary dictionaryWithObjects:@[self.dk_UserProductPackageMdl.amount,self.dk_UserProductPackageMdl.id,self.dk_UserProductPackageMdl.term] forKeys:@[@"amount",@"id",@"term"]];
        [totalLoanInfos addObject:productPackageDic];
        parmDic = [NSDictionary dictionaryWithObject:totalLoanInfos forKey:@"reqList"];
    }else {
        for (DKUserProductPackageModel * dk_UserProductPackageMdl in self.selectProductList) {
            NSDictionary *productPackageDic = [NSDictionary dictionaryWithObjects:@[dk_UserProductPackageMdl.amount,dk_UserProductPackageMdl.id,dk_UserProductPackageMdl.term] forKeys:@[@"amount",@"id",@"term"]];
            [totalLoanInfos addObject:productPackageDic];
        }
        parmDic = [NSDictionary dictionaryWithObject:totalLoanInfos forKey:@"reqList"];
    }
    [DKPublicHttp apiTotalLoanInfoForAPI:parmDic complete:^(DKLoanDetailModel * _Nonnull dk_LoanDetailMdl) {
        weakSelf.dk_LoanDetailMdl = dk_LoanDetailMdl;
        [weakSelf.homeLoanDetailTableView reloadData];
    }];
}
- (void)requestApplyLoan{
    [self requestUserBrhavRecordApplyLoan:DK_ActionApply_Loan_Click withParmDic:@{}];
    
    WeakSelf(self)
    NSDictionary * parmDic;
    NSMutableArray *applyLoans = [[NSMutableArray alloc] init];
    if(self.dk_UserProductPackageMdl){
        NSDictionary *productPackageDic = [NSDictionary dictionaryWithObjects:@[self.dk_UserProductPackageMdl.amount,self.dk_UserProductPackageMdl.isReloan,self.dk_UserProductPackageMdl.id,self.dk_UserProductPackageMdl.term,self.dk_UserProductPackageMdl.termUnit] forKeys:@[@"applyAmount",@"isReloan",@"productId",@"term",@"termUnit"]];
        [applyLoans addObject:productPackageDic];
        parmDic = [NSDictionary dictionaryWithObject:applyLoans forKey:@"gwApplyReqList"];
    }else{
        for (DKUserProductPackageModel * dk_UserProductPackageMdl in self.selectProductList) {
            NSDictionary *productPackageDic = [NSDictionary dictionaryWithObjects:@[dk_UserProductPackageMdl.amount,dk_UserProductPackageMdl.isReloan,dk_UserProductPackageMdl.id,dk_UserProductPackageMdl.term,dk_UserProductPackageMdl.termUnit] forKeys:@[@"applyAmount",@"isReloan",@"productId",@"term",@"termUnit"]];
            [applyLoans addObject:productPackageDic];
        }
        parmDic = [NSDictionary dictionaryWithObject:applyLoans forKey:@"gwApplyReqList"];
    }
    [DKPublicHttp apiLoanApplyForAPI:parmDic complete:^(BOOL isScore) {
        [weakSelf requestUserBrhavRecordApplyLoan:DK_ActionApply_Loan_Success withParmDic:@{}];
        //风控埋点
        [weakSelf requestBeginLoan:YES];
        
        DKHomeLoanScuessController *dk_HomeLoanScuessCtrl = [[DKHomeLoanScuessController alloc] init];
        dk_HomeLoanScuessCtrl.isShowScore = isScore;
        [weakSelf.navigationController pushViewController:dk_HomeLoanScuessCtrl animated:YES];
    }];
}
- (void)requestUserBrhavRecordApplyLoan:(NSString *)action withParmDic:(NSDictionary *)parmDic{
    [DKPublicHttp apiUserBrhavRecordForAPI:parmDic withPageName:DK_ApplyLoan withAction:action complete:^{
            
    } fail:^{
            
    }];
}
#pragma mark -- UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return DK_IsWidthScale(513)+DK_IsWidthScale(40)*self.dk_LoanDetailMdl.pairs.count+DK_SafeAreaTop+DK_SafeAreaBottom;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WeakSelf(self)
    DKAuthLoanDetailCell *dk_AuthLoanDetailCell = [DKAuthLoanDetailCell authLoanDetailCell:tableView];
    dk_AuthLoanDetailCell.dk_LoanDetailMdl = self.dk_LoanDetailMdl;
    dk_AuthLoanDetailCell.GoPrivacyAgreementBlock = ^{
        DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] initWithUrl:DK_PrivacyAgreement withIsNavBar:YES];
        [weakSelf.navigationController pushViewController:dk_WebViewCtrl animated:YES];
    };
    dk_AuthLoanDetailCell.GoLoanAgreementBlock = ^{
        DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] initWithUrl:DK_LoanAgreement withIsNavBar:YES];
        [weakSelf.navigationController pushViewController:dk_WebViewCtrl animated:YES];
    };
    dk_AuthLoanDetailCell.GoLoanApplyBlock = ^{
        [weakSelf requestApplyLoan];
    };
    return dk_AuthLoanDetailCell;
}
@end
